package com.zr.bookcorner.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.zh.baselibrary.base.BaseActivity;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.constant.ExtraName;
import com.zh.baselibrary.utils.ClickUtils;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.TTSUtils;
import com.zh.baselibrary.views.ToastView;
import com.zr.andealib.util.HardEvent;
import com.zr.andealib.util.constant.AndeaConstant;
import com.zr.andealib.util.model.InventoryReport;
import com.zr.bookcorner.BookApplication;
import com.zr.bookcorner.R;
import com.zr.bookcorner.constant.Constant;
import com.zr.bookcorner.dialog.ManagerVerificationDialog;
import com.zr.bookcorner.presenter.ChoosePresenter;
import com.zr.bookcorner.presenter.view.ChooseView;
import com.zr.bookcorner.service.VoiceEvent;
import com.zr.bookcorner.view.RoundBackgroundColorSpan;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class ManagerActivity extends BaseActivity<ChoosePresenter> implements ChooseView,View.OnClickListener {

    private int buttonCount = 3;
    private int boxNum;
    private ManagerVerificationDialog mVerificationDialog;
    BroadcastReceiver mBroadcastReceiver;
    @Override
    protected int getLayoutId() {
        if (BookApplication.isBig()) {
            buttonCount = 16;
            return R.layout.manager_activity_big;
        }
        return R.layout.manager_activity;
    }

    @Override
    protected void init() {
        for(int i = 1;i<=buttonCount;i++){
            TextView textView = findViewById(getId(("btn_cabinet"+i)));
            textView.setOnClickListener(this);
            SpannableStringBuilder style = new SpannableStringBuilder(textView.getText().toString());
            style.setSpan(new RoundBackgroundColorSpan(Color.parseColor("#004F80"),Color.WHITE),0,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(style);
        }
        findViewById(R.id.ivTitleRight).setOnClickListener(this);
        if(BookApplication.isMini()){
            TextView textView = findViewById(R.id.btn_cabinet1);
            TextView textView2 = findViewById(R.id.btn_cabinet2);
            TextView textView3 = findViewById(R.id.btn_cabinet3);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
            textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
            textView3.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
        }
        mPresenter.setType(1);
    }
    private int getId(String resName) {
        return getResources().getIdentifier(resName, "id",getPackageName());
    }
    @Override
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) {
            if(v.getId() == R.id.ivTitleRight){
                IntentUtils.instance().startActivity(this,HelpActivity.class);
            }else {
                boxNum = Integer.parseInt((String) v.getTag());
                mPresenter.checkDoorStatus(boxNum);
            }
        }
    }
    @Override
    protected ChoosePresenter createPresenter() {
        return new ChoosePresenter();
    }

    @Override
    public void showDialog() {
        mVerificationDialog = new ManagerVerificationDialog(this, R.style.DialogTheme, null);
        mVerificationDialog.setShowQRCode(true);
        mVerificationDialog.show();
    }

    @Override
    public void openSuccess() {
        dismissDialog();
        Bundle bundle = new Bundle();
        bundle.putInt(ExtraName.KEY_DATA,boxNum);
        IntentUtils.instance().startActivity(this,ManagerDetailActivity.class,bundle);
      //  TTSUtils.getInstance().speak(getString(R.string.manager_open_success));
        EventBus.getDefault().post(new VoiceEvent(getString(R.string.manager_open_success)));
    }

    @Override
    public void openError() {
     //   TTSUtils.getInstance().speak(getString(R.string.manager_open_error));
        EventBus.getDefault().post(new VoiceEvent(getString(R.string.manager_open_error)));
        dismissDialog();
    }
    @Override
    public void defyError() {
       // TTSUtils.getInstance().speak(getString(R.string.manager_defy_error));
        EventBus.getDefault().post(new VoiceEvent(getString(R.string.manager_defy_error)));
        dismissDialog();
    }

    private void dismissDialog() {
        if (mVerificationDialog != null && mVerificationDialog.isShowing())
            mVerificationDialog.cancel();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.initLock();

        //同时注册
        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                dismissDialog();
                mPresenter.open(boxNum);
            }
        };
        registerReceiver(mBroadcastReceiver, new IntentFilter(Constant.BROAT_CAST_ACTION_QRCODE_LOGIN));
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onDestroy();
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }
    }
    @Override
    public void onHint(String hint) {
        ToastView.showShort(hint);
        dismissDialog();
    }

    @Override
    public void onSuccess(Object object) {
        dismissDialog();
    }

}
